package http

import (
	"gitee.com/itgsyang/restful-api-demo/apps/hosts"
	"github.com/gin-gonic/gin"
	"net/http"
)

type HostHandler struct {
	svc hosts.HostEntityService
}

func NewHostHandler(svc hosts.HostEntityService) *HostHandler {
	return &HostHandler{svc: svc}
}

func (h *HostHandler) Register(r gin.IRouter) {
	r.POST("/", h.createHost)
}

func (h *HostHandler) createHost(ctx *gin.Context) {
	var host hosts.Host
	if err := ctx.Bind(&host); err != nil {
		ctx.JSON(http.StatusBadRequest, map[string]interface{}{
			"code": 0,
			"data": nil,
			"msg":  "参数无效",
		})
		return
	}
	createHost, err := h.svc.CreateHost(ctx.Request.Context(), host)
	if err != nil {
		ctx.JSON(http.StatusOK, map[string]interface{}{
			"code": 0,
			"data": nil,
			"msg":  "未知错误",
		})
		return
	}

	ctx.JSON(http.StatusOK, map[string]interface{}{
		"code": 1,
		"data": createHost,
		"msg":  "创建成功",
	})
	return
}
